import com.sun.scenario.effect.Merge;

import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
public class txtReader {
    public String filePath="chart.txt";
    private BufferedReader BfReader;
    public void txtReader(){
        try{
            File file=new File(filePath);
            InputStreamReader IpReader = new InputStreamReader(new FileInputStream(file));
            BfReader = new BufferedReader(IpReader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void fresh(){
        try{
            File file=new File("chart.txt");
            InputStreamReader IpReader = new InputStreamReader(new FileInputStream(file));
            BfReader = new BufferedReader(IpReader);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void addName(String addFilePath){
        try {
            File file = new File(addFilePath);
            InputStreamReader tempIpReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader tempBfReader = new BufferedReader(tempIpReader);
            String line;
            BufferedWriter bWriter = new BufferedWriter(new FileWriter(filePath,true));
            bWriter.newLine();
            while(( line=tempBfReader.readLine()) != null){
                bWriter.write(line+"\n");
            }
            bWriter.flush();
            bWriter.close();
            fresh();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void addRandomScore(){
        try{
            String line;
            ArrayList lines=new ArrayList();
            Random rnd = new Random();
            lines.add(BfReader.readLine());
            while((line=BfReader.readLine()) != null){
                int totalScore = 0;
                int Score;
                for(int i=0;i<5;i++){
                    Score = rnd.nextInt(50)+50;
                    line+="\t";
                    line+=Score;
                    totalScore += Score;
                }
                line += ("\t"+totalScore);
                lines.add(line);
            }
            BufferedWriter bWriter = new BufferedWriter(new FileWriter(filePath));
            for(int i=0;i<lines.size();i++){
                bWriter.write(lines.get(i).toString());
                bWriter.newLine();
            }
            bWriter.flush();
            bWriter.close();
            fresh();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public float[] calcAverage(){
        float[] averages={0f,0f,0f,0f,0f};
        int student = 0;
        try{
            String line;
            while((line=BfReader.readLine())!=null){
                float Score = 0f;
                if(student==0){
                    student++;
                    continue;
                }
                for(int i = 0;i<5;i++){
                    averages[i] +=Float.parseFloat((line.split("\t")[i + 2]));
                }
                student++;
            }
            BufferedWriter bWriter = new BufferedWriter(new FileWriter(filePath,true));
            bWriter.write("Average:");
            for(int i =0;i<5;i++) {
                averages[i] /= student;
                bWriter.write("\t"+averages[i]);
            }
            bWriter.flush();
            bWriter.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        fresh();
        return averages;
    }
    public void outPutSort(){
        try{
            ArrayList<String> lines = new ArrayList<String>();
            String line;
            BfReader.readLine();
            while((line=BfReader.readLine())!=null){
                lines.add(line);
            }
            String average=lines.get(lines.size()-1);
            lines.remove(lines.size()-1);
            String[] sort=new String[lines.size()];
            String[] sorted=MergeSort.MergeSort(lines.toArray(sort));
            File outPutFile=new File("output.txt");
            if(!outPutFile.exists()){
                outPutFile.createNewFile();
            }
            FileWriter writer=new FileWriter(outPutFile);
            BufferedWriter bWriter = new BufferedWriter(writer);
            bWriter.write("Name\tNo\tChinese\tEnglish\tMath\tPhysics\tChemistry\tTotal\n");
            for(int i=0;i<sorted.length;i++){
                bWriter.write(sorted[i]);
                bWriter.newLine();
            }
            bWriter.write(average);
            bWriter.flush();
            bWriter.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
